package com.videoweb.domain.agent.entity;

import com.videoweb.domain.agent.valueobject.ModelConfig;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;

import java.time.LocalDateTime;

/**
 * Agent聚合根
 * 智能体实体，包含完整的业务逻辑和生命周期管理
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Agent {
    
    private Long id;
    private String name;
    private String description;
    private String avatar;
    private String agentType; // general, coder, analyst, writer
    private ModelConfig modelConfig;
    private String systemPrompt;
    private Boolean codeGenEnabled;
    private Boolean ragEnabled;
    private Boolean memoryEnabled;
    private Integer status; // 0:禁用, 1:启用
    private Long userId;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    /**
     * 激活Agent
     */
    public void activate() {
        if (this.status == 0) {
            this.status = 1;
            this.updatedAt = LocalDateTime.now();
        }
    }
    
    /**
     * 禁用Agent
     */
    public void deactivate() {
        if (this.status == 1) {
            this.status = 0;
            this.updatedAt = LocalDateTime.now();
        }
    }
    
    /**
     * 更新配置
     */
    public void updateConfig(ModelConfig newConfig) {
        if (newConfig != null) {
            this.modelConfig = newConfig;
            this.updatedAt = LocalDateTime.now();
        }
    }
    
    /**
     * 更新系统提示词
     */
    public void updateSystemPrompt(String newPrompt) {
        if (newPrompt != null && !newPrompt.trim().isEmpty()) {
            this.systemPrompt = newPrompt;
            this.updatedAt = LocalDateTime.now();
        }
    }
    
    /**
     * 检查是否可以进行对话
     */
    public boolean canChat() {
        return this.status == 1 && this.modelConfig != null;
    }
    
    /**
     * 检查是否启用代码生成
     */
    public boolean isCodeGenerationEnabled() {
        return this.codeGenEnabled != null && this.codeGenEnabled;
    }
    
    /**
     * 检查是否启用RAG功能
     */
    public boolean isRagEnabled() {
        return this.ragEnabled != null && this.ragEnabled;
    }
    
    /**
     * 克隆Agent
     */
    public Agent clone(String newName, Long newUserId) {
        return Agent.builder()
                .name(newName)
                .description(this.description)
                .agentType(this.agentType)
                .modelConfig(this.modelConfig)
                .systemPrompt(this.systemPrompt)
                .codeGenEnabled(this.codeGenEnabled)
                .ragEnabled(this.ragEnabled)
                .memoryEnabled(this.memoryEnabled)
                .status(1) // 新克隆的Agent默认启用
                .userId(newUserId)
                .createdAt(LocalDateTime.now())
                .updatedAt(LocalDateTime.now())
                .build();
    }
    
    /**
     * 验证Agent数据完整性
     */
    public boolean isValid() {
        return name != null && !name.trim().isEmpty() 
            && userId != null 
            && modelConfig != null 
            && modelConfig.isValid();
    }
}